package com.realife.eson.common.asserts;


import com.realife.eson.common.enums.ResponseEnum;
import com.realife.eson.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;

@Slf4j
public class Assert {

    /**
     * 断言对象不为空
     * obj 为空则抛异常
     *
     * @param obj
     * @param responseEnum
     */
    public static void notNull(Object obj, ResponseEnum responseEnum) {
        if (obj == null) {
            log.info("obj is null.....................{}", responseEnum.getMessage());
            throw new BusinessException(responseEnum);
        }
    }


    /**
     * 断言对象为空
     * 如果对象obj不为空，则抛出异常
     *
     * @param object
     * @param responseEnum
     */
    public static void isNull(Object object, ResponseEnum responseEnum) {
        if (object != null) {
            log.info("obj is not null......{}",responseEnum.getMessage());
            throw new BusinessException(responseEnum);
        }
    }

    /**
     * 断言表达式为真
     * 如果不为真，则抛出异常
     *
     * @param expression 是否成功
     */
    public static void isTrue(boolean expression, ResponseEnum responseEnum) {
        if (!expression) {
            log.info("fail...............{}", responseEnum.getMessage());
            throw new BusinessException(responseEnum);
        }
    }


    /**
     * 断言表达式为假
     * 如果不为假，则抛出异常
     *
     * @param expression 是否为假
     */
    public static void isFalse(boolean expression, ResponseEnum responseEnum) {
        if (expression) {
            log.info("结果为真, 应该为假才符合预期...............{}", responseEnum.getMessage());
            throw new BusinessException(responseEnum);
        }
    }


    /**
     * 断言两个对象不相等
     * 如果相等，则抛出异常
     *
     * @param m1
     * @param m2
     * @param responseEnum
     */
    public static void notEquals(Object m1, Object m2, ResponseEnum responseEnum) {
        if (m1.equals(m2)) {
            log.info("equals...............{}", responseEnum.getMessage());
            throw new BusinessException(responseEnum);
        }
    }

    /**
     * 断言两个对象相等
     * 如果不相等，则抛出异常
     *
     * @param m1
     * @param m2
     * @param responseEnum
     */
    public static void equals(Object m1, Object m2, ResponseEnum responseEnum) {
        if (!m1.equals(m2)) {
            log.info("not equals...............{}",responseEnum.getMessage());
            throw new BusinessException(responseEnum);
        }
    }

    /**
     * 断言参数不为空
     * 如果为空，则抛出异常
     *
     * @param s
     * @param responseEnum
     */
    public static void notEmpty(String s, ResponseEnum responseEnum) {
        if (StringUtils.isEmpty(s)) {
            log.info("is empty...............{}", responseEnum.getMessage());
            throw new BusinessException(responseEnum);
        }
    }


    /**
     * 有言在先: 有一个不合法都不行
     *
     * @param responseEnum
     * @param params
     */
    public static void legalParams(ResponseEnum responseEnum, Object... params) {
        boolean flag = false;
        for (Object param : params) {
            if (null == param) {
                log.info("[**===================> param null **]");
                flag = true;
                break;
            }
            if (param instanceof String) {
                if (StringUtils.isEmpty(param)) {
                    log.info("[**===================> str empty **]");
                    flag = true;
                    break;
                }
            }
            if (param instanceof Collection) {
                if (CollectionUtils.isEmpty((Collection<?>) param)) {
                    log.info("[**===================> collection empty **]");
                    flag = true;
                    break;
                }
            }
            if (param instanceof Map) {
                if (CollectionUtils.isEmpty(((Map) param))) {
                    log.info("[**===================> map empty **]");
                    flag = true;
                    break;
                }
            }
            //...
        }
        if (flag) {
            log.info("[**===================> 出现非法参数(null or empty)... {}**]", responseEnum.getMessage());
            throw new BusinessException(responseEnum);
        }
    }


    public static void main(String[] args) {
        //legalParams(ResponseEnum.PARAM_ILLEGAL, null, null, null,"aaa", new ArrayList<>(), "");
        legalParams(ResponseEnum.PARAM_ILLEGAL, null, null, null);
        System.out.println("ok~");
    }
}
